package Controller;
use Data::Dumper;

use Moose;

has module => ( is => 'rw' );
has action	=> ( is => 'rw' );
has args 	=> ( is => 'ro', default => sub { {} } );
has _args 	=> ( is => 'ro', default => sub { {} } );
has triggers => ( 
	is => 'ro', 
	isa => 'ArrayRef',
	default => sub { [] } , 
	require => 1, 
);

sub invoke
{
	my $self = shift;
	my $request = shift;

	my $module = $self->module;
	my $action = $self->action;

	eval "require $module";
	
	while ( my ($k, $v) = each %{$self->args} ){
		$request->{global_vars}{$k} = $v;
	}
	$request->server->log->warn("controller: $module, action: $action");
	
	for my $trigger (@{ $self->triggers }){
		no strict 'refs';
		my $response = $trigger->($request, $self->args);
		return $response if ref $response eq 'Response';	
	}
	
	return $module->$action($request, $self->args);
}
1;
